package com.kun.web.controller.system;

import com.kun.utils.AjaxResult;
import com.kun.auth.LoginUser;
import com.kun.domain.SysUser;
import com.kun.service.service.ISysUserService;
import com.kun.service.service.SysPermissionService;
import com.kun.utils.PageUtils;
import com.kun.utils.SecurityUtils;
import com.kun.vo.UserInfo;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/sys_user")
public class UserController {

    private final SysPermissionService permissionService;
    private final ISysUserService sysUserService;

    public UserController(SysPermissionService permissionService, ISysUserService sysUserService) {
        this.permissionService = permissionService;
        this.sysUserService = sysUserService;
    }

    /**
     * 获取用户列表
     */
    @GetMapping("/list_data")
    public ResponseEntity<AjaxResult> list() {
        List<SysUser> list = sysUserService.list();
        return AjaxResult.success(list);
    }

    /**
     * 根据条件分页查询用户列表
     */
    @GetMapping("/list")
    public ResponseEntity<AjaxResult> list(SysUser sysUser) {
        PageUtils.startPage();
        List<SysUser> list = sysUserService.selectUserList(sysUser);
        // 这里会设置分页的总数
        AjaxResult ajaxResult = AjaxResult.getDataTable(list);
        ajaxResult.put("data", sysUserService.addInfo(list));
        return new ResponseEntity<>(ajaxResult, HttpStatus.OK);
    }

    /**
     * 获取用户信息
     */
    @PostMapping("/getInfo")
    public ResponseEntity<AjaxResult> getInfo() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Assert.isTrue(loginUser != null, "用户信息为空");
        SysUser user = loginUser.getUser();
        // 角色集合
//        Set<String> roles = permissionService.getRolePermission(user);
//        // 权限集合
//        Set<String> permissions = permissionService.getMenuPermission(user);
//        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
//        stringObjectHashMap.put("user", user);
//        stringObjectHashMap.put("roles", roles);
//        stringObjectHashMap.put("permissions", permissions);
        UserInfo userInfo = new UserInfo(user.getName(), user.getAvatar(), user.getEmail(), user.getPhonenumber(),
                "admin");
        return AjaxResult.success(userInfo);
    }

}
